﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DDD.Core;

using FloatComparer = DDDTest.Helper.FloatComparer;

namespace DDDTest.Core {
    [TestClass]
    public class TestFeedbackBuffer {
        [TestMethod]
        public void Test_New1 () {
            var fbuf = new FeedbackBuffer ();

            Assert.AreEqual (0, fbuf.FeedbackVaryingCount);
            Assert.AreEqual (0, fbuf.FeedbackVaryings.Count ());
        }

        [TestMethod]
        public void Test_Indexer () {
            var fbuf = new FeedbackBuffer ();
            var varry = new VertexArray<float> (4, 3);
            fbuf.AddVertexArray ("Hello world", varry);

            Assert.AreEqual (1, fbuf.FeedbackVaryingCount);
            Assert.AreEqual (1, fbuf.FeedbackVaryings.Count ());

            var vary = fbuf["Hello world"];
            Assert.AreEqual ("Hello world", vary.Name);
            Assert.AreEqual (varry, vary.VertexArray);
            Assert.AreEqual (null, vary.CopyTo);
        }

        [TestMethod]
        public void Test_SetVertexArray () {
            var fbuf = new FeedbackBuffer ();
            var varry = new VertexArray<float> (4, 3);

            fbuf.AddVertexArray ("Hello world", varry);

            Assert.AreEqual (1, fbuf.FeedbackVaryingCount);
            Assert.AreEqual (1, fbuf.FeedbackVaryings.Count ());

            var fvar = fbuf.GetFeedbackVarying ("Hello world");
            Assert.AreEqual ("Hello world", fvar.Name);
            Assert.AreEqual (varry, fvar.VertexArray);
            Assert.AreEqual (null, fvar.CopyTo);
        }

        [TestMethod]
        public void Test_SetVertexArray_CopyTo () {
            var fbuf = new FeedbackBuffer ();
            var varry1 = new VertexArray<float> (4, 3);
            var varry2 = new VertexArray<float> (4, 3);

            fbuf.AddVertexArray ("Hello world", varry1, varry2);

            Assert.AreEqual (1, fbuf.FeedbackVaryingCount);
            Assert.AreEqual (1, fbuf.FeedbackVaryings.Count ());

            var fvar = fbuf.GetFeedbackVarying ("Hello world");
            Assert.AreEqual ("Hello world", fvar.Name);
            Assert.AreEqual (varry1, fvar.VertexArray);
            Assert.AreEqual (varry2, fvar.CopyTo);
        }

        [TestMethod]
        public void Test_RemoveVertexArray () {
            var fbuf = new FeedbackBuffer ();
            var varry1 = new VertexArray<float> (4, 3);
            var varry2 = new VertexArray<float> (4, 3);

            fbuf.AddVertexArray ("Hello world", varry1, varry2);
            fbuf.RemoveVertexArray ("Hello world");

            Assert.AreEqual (0, fbuf.FeedbackVaryingCount);
        }

        [TestMethod]
        public void Test_References () {
            var fbuf = new FeedbackBuffer ();
            var varry1 = new VertexArray<float> (4, 3);
            var varry2 = new VertexArray<float> (4, 3);

            fbuf.AddVertexArray ("Hello", varry1);
            fbuf.AddVertexArray ("World", varry2);

            var objs = fbuf.References;

            Assert.AreEqual (2, objs.Count ());
            Assert.AreEqual (true, objs.All (x => x != null));
        }

        [TestMethod]
        public void Test_Find () {
            var fbuf = new FeedbackBuffer ();
            var varry1 = new VertexArray<float> (4, 3);
            var varry2 = new VertexArray<float> (4, 3);

            fbuf.AddVertexArray ("Hello", varry1);
            fbuf.AddVertexArray ("World", varry2);

            varry1.UserID = 101;

            Assert.AreEqual (varry1, fbuf.Find (101));
        }
    }
}
